// pages/mine/edit.js
const api = require('../../../utils/api')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    professionType: ['哲学', '经济学', '法学', '教育学', '文学', '历史学', '理学', '工学', '农学', '医学', '军事学', '管理学和艺术学'],
    education: ['本科', '大专', '博士', '高中', '博士后'],
    grade: ['一年级', '二年级', '三年级', '四年级', '五年级'],
    province: ['北京市', '天津市', '上海市', '重庆市', '河北省', '山西省', '辽宁省', '吉林省', '黑龙江省', '江苏省', '浙江省', '安徽省', '福建省', '江西省', '山东省', '河南省', '湖北省', '湖南省', '广东省', '海南省', '四川省', '贵州省', '云南省', '陕西省', '甘肃省', '青海省', '台湾省', '内蒙古自治区', '广西壮族自治区', '西藏自治区', '宁夏回族自治区', '新疆维吾尔自治区', '香港特别行政区', '澳门特别行政区'],
    userInfo: {},
    formData: {
      sex: 1
    },
    modalName: '', //modal
    modalType: ['全国', '省级', '校级'],
    modalPrize: {
      ind: ''
    },
    editType: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user = app.globalData.user;
    let title = '编辑信息';
    // if(user.register=='no'){
    //   title='注册';
    // }
    wx.setNavigationBarTitle({
      title: title,    //页面标题
      success: () => { },   //接口调用成功的回调函数
      fail: () => { },      //接口调用失败的回调函数
      complete: () => { }   //接口调用结束的回调函数（调用成功','失败都会执行）
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (option) {
    let editType = this.data.editType;
    console.log()
    //学校编辑
    let school = this.data.changeSchool;
    if (editType == 'school' && school) {
      this.changeForm('school_name', school.name)
      this.changeForm('school_id', school.id)
    }
    //技能
    let skill = this.data.skills_good;
    if (editType == 'good' && skill) {
      this.changeForm('skills_good', skill);
    }
    let skill2 = this.data.skills_know;
    if (editType == 'know' && skill2) {
      this.changeForm('skills_know', skill2);
    }

    console.log(editType, skill, skill2)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 性别选择
  changeSex: function (op) {
    let sex = op.detail.value ? 1 : 0;
    this.changeForm('sex', sex)
  },
  changeProfessionType: function (opt) {
    this.changeForm('profession_type', this.data.professionType[opt.detail.value])
  },
  changeEducation: function (opt) {
    this.changeForm('education', this.data.education[opt.detail.value])
  },
  changeGrade: function (opt) {
    this.changeForm('grade', this.data.grade[opt.detail.value])
  },
  changeProvince: function (opt) {
    this.changeForm('province', this.data.province[opt.detail.value])
  },
  changeSchool: function (option) {
    wx.navigateTo({
      url: '/pages/mine/edit_school/index',
    })
  },
  changeSkills: function (dom) {
    let type = dom.currentTarget.dataset.type || 'good';
    wx.navigateTo({
      url: '/pages/mine/edit_skills/index?type=' + type,
    })
  },
  addPrize: function (dataForm) {
    let formSub = dataForm.detail.value;
    if (formSub.prizeName == '') {
      return;
    }
    let prize = this.data.formData.prize || [];
    let ind = this.data.modalPrize ? this.data.modalPrize.ind : '';
    if (ind !== '') {
      prize.splice(ind, 1, formSub)
    } else {
      prize.push(formSub)
    }
    this.changeForm('prize', prize);
    this.hideModal();
  },
  delPrize: function (e) {
    let ind = e.currentTarget.dataset.ind;
    let prize = this.data.formData.prize;
    prize.splice(ind, 1);
    this.changeForm('prize', prize);
  },
  changeForm: function (key, value) {
    let form = this.data.formData;
    form[key] = value
    this.setData({
      formData: form
    }, () => {
      //console.log(this.data.formData)
    })
  },
  formSubmit: function (data) {
    let formSub = data.detail.value;
    let contact = [
      {
        'name': 'qq',
        'value': formSub.qq
      },
      {
        'name': 'weixin',
        'value': formSub.weixin
      },
      {
        'name': 'phone',
        'value': formSub.phone
      }
    ]
    formSub.contact = contact;
    let subData = Object.assign(formSub, this.data.formData);
    api.updateUserinfo(subData).then((res) => {
      let { errcode, msg } = res
      if (errcode === 0) {
        wx.showToast({
          title: '操作成功',
          icon: 'none',
          image: '',
          duration: 1500,
          mask: false,
          success: (result) => {
            // todo 我的信息页面
            // wx.navigateTo({
            //   url: '/pages/mine/mine'
            // });
            setTimeout(() => {
              wx.switchTab({
                url: '/pages/mine/mine',
              });
            }, 1000)
          }
        });

      }
    })
  },
  // modal
  showModal(e) {
    let dataset = e.currentTarget.dataset;
    let info = dataset.info;
    if (info) {
      info.ind = dataset.ind;
    }
    this.setData({
      modalName: dataset.target,
      modalPrize: info
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    }, () => {
      setTimeout(() => {
        this.setData({
          modalPrize: {}
        })
      }, 100)

    })
  },
})